package com.hq.report.actions;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.eredlab.g4.ccl.datastructure.Dto;
import org.eredlab.g4.ccl.datastructure.impl.BaseDto;
import org.eredlab.g4.ccl.json.JsonHelper;
import org.eredlab.g4.rif.report.fcf.FcfDataMapper;
import org.eredlab.g4.rif.report.fcf.GraphConfig;
import org.eredlab.g4.rif.report.fcf.Set;
import org.eredlab.g4.rif.web.BaseAction;
import org.eredlab.g4.rif.web.CommonActionForm;

public class ReportAction extends BaseAction {
	private static final String[] moneyname=new String[]{"合同金额","实际金额","预算金额"};
	//初始化年度费用类型统计页面
	public ActionForward init(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("提示：进入图表的初始化页面。。。。。。。。。");
		//将图表数据转为Flash能解析的XML资料格式
		String xmlString = this.getXML1(new BaseDto());
		request.setAttribute("xmlString",xmlString);
		return mapping.findForward("init");
	}
	//由于多处用到重复代码，因此将其提取出来作为单独的方法共外界调用！
	private String getXML1(Dto dto)
	{
		List list=null;
		if(dto.getAsInteger("year")==null)
		{
			//默认情况下加载当年的统计图表
			list = g4Reader.queryForList("report.niandu", Calendar.getInstance().get(Calendar.YEAR));
		}
		else
		{
			list=this.g4Reader.queryForList("report.niandu", dto.getAsInteger("year"));
		}
		System.out.println("查询出来的数据！");
		for(int i=0;i<list.size();i++)
		{
			System.out.println("第"+i+"个费用类型名称是："+((BaseDto)(list.get(i))).getAsString("tp_name"));
		}
		System.out.println("查询出来的数据！");
		//实例化一个图形配置对象
		GraphConfig graphConfig = new GraphConfig();
		//主标题
		graphConfig.setCaption(dto.getAsString("year")+"年度费用统计图表（单位：元）");
		//X坐标轴名称
		graphConfig.setXAxisName("费用类型");
		graphConfig.setYAxisName("费用金额");
		//数字值前缀
		//graphConfig.setNumberPrefix("$");
		List dataList = new ArrayList();
		List types=this.g4Reader.queryForList("report.feiyongtype");
		System.out.println("模板费用类型数据"+types.size());
		int cont=0;
		for (int j = 0; j < list.size(); j++)
		{
			Dto dto1 = (BaseDto)list.get(j);
			//实例化一个图表元数据对象
			Set set = new Set();	
			set.setName(dto1.getAsString("tp_name")); //名称
			set.setValue(dto1.getAsString("total")); //数据值
			dataList.add(set);
			//能在这个for循环中执行的都是可以查到的即都是有数据的，因此我们立刻将其从 types集合中移除，那么剩下的就是没有值的需要我们自己构造的，技巧！
			Dto remove=new BaseDto();
			remove.put("tp_name",dto1.getAsString("tp_name"));
			types.remove(remove);
			cont++;
			remove=null;
		}
		System.out.println("移除了"+cont+"个！");
		System.out.println("模板费用类型数据"+types.size());
		for(int i=0;i<types.size();i++)
		{
			System.out.println("======================================正在装载："+((Dto)types.get(i)).getAsString("tp_name"));
			System.out.println("手动装载的类型有："+((Dto)types.get(i)).getAsString("tp_name"));
			//构造没有查询到的费用类型，也要为其构造图形！
			Set set = new Set();	
			set.setName(((Dto)types.get(i)).getAsString("tp_name")); //名称
			set.setValue("0"); //数据值
			dataList.add(set);
		}
		//将图表数据转为Flash能解析的XML资料格式
		String xmlString = FcfDataMapper.toFcfXmlData(dataList, graphConfig);
		return xmlString;
	}
	//查询年度费用数据统计数据
	public ActionForward queryNianDuTotalByType(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("提示：进入图表的表格查询逻辑。。。。。。。。。");
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		List list=null;
		int year;
		if(dto.getAsInteger("year")==null)
		{
			year=Calendar.getInstance().get(Calendar.YEAR);
			list=this.g4Reader.queryForList("report.niandu",year);
		}
		else
		{
			year=dto.getAsInteger("year");
			list=this.g4Reader.queryForList("report.niandu",year);
		}
		//得到费用的所有类型值然后循环匹配
		List types=this.g4Reader.queryForList("report.feiyongtype");
		if(types.size()!=list.size())
		{
			for (int j = 0; j < list.size(); j++)
			{
				Dto dto1 = (BaseDto)list.get(j);
				//能在这个for循环中执行的都是可以查到的即都是有数据的，因此我们立刻将其从 types集合中移除，那么剩下的就是没有值的需要我们自己构造的，技巧！
				Dto remove=new BaseDto();
				remove.put("tp_name",dto1.getAsString("tp_name"));
				types.remove(remove);
				remove=null;
			}
			//给不存在的手动添加默认值！
			for(int i=0;i<types.size();i++)
			{
				Dto d=new BaseDto();
				d.put("tp_name",((Dto)types.get(i)).getAsString("tp_name"));
				d.put("total", "0");
				d.put("time",year+"");
				list.add(d);
			}
		}
		String jsonString = JsonHelper.encodeList2PageJson(list,list.size(),null);
		this.write(jsonString, response);
		return null;
	}
	
	//查询年度费用统计数据并格式化成xml用于显示图表
	public ActionForward queryNianDuXml(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("提示：正在修改图表参数。。。。。。");
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		//将图表数据转为Flash能解析的XML资料格式
		String xmlString = this.getXML1(dto);
		Dto outDto = new BaseDto();
		outDto.put("success", new Boolean(true));
		outDto.put("xmlstring", xmlString);
		write(JsonHelper.encodeObject2Json(outDto), response);
		return mapping.findForward(null);
	}
	
	public ActionForward init2(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		//将图表数据转为Flash能解析的XML资料格式
		String xmlString = this.getXML2(new BaseDto());
		request.setAttribute("xmlString",xmlString);
		return mapping.findForward("init2");
	}
	//查询年度费用数据统计数据
	public ActionForward queryMoneyType(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("提示：正在查询费用类型名以及金额。。。。。。。。。");
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		Integer year=dto.getAsInteger("year");
		if(year==null)
		{
			year=Calendar.getInstance().get(Calendar.YEAR);
		}
		
		
		List list=new ArrayList();
		
		
		Dto resDto=(Dto)this.g4Reader.queryForObject("report.hetongmoney", year);
		//这样做可以确保统计后是否有次费用都会显示，只不过将其当做0处理而已！
		if(resDto==null)
		{
			resDto=new BaseDto();
		}
		resDto.put("mname",moneyname[0]);
		System.out.println("合同金额是："+resDto);
		list.add(resDto);
		
		
		Dto resDto2=(Dto)this.g4Reader.queryForObject("report.shijimoney",year);
		if(resDto2==null)
		{
			resDto2=new BaseDto();
		}
		resDto2.put("mname",moneyname[1]);
		System.out.println("实际金额是："+resDto2);
		list.add(resDto2);
		
		
		Dto resDto3=(Dto)this.g4Reader.queryForObject("report.yusuanmoney",year);
		if(resDto3==null)
		{
			resDto3=new BaseDto();
		}
		resDto3.put("mname",moneyname[2]);
		System.out.println("预算金额是："+resDto3);
		list.add(resDto3);
		
		String jsonString = JsonHelper.encodeList2PageJson(list,list.size(), null);
		this.write(jsonString, response);
		return null;
	}
	public String getXML2(Dto dto)
	{
		List list=null;
		Integer year=dto.getAsInteger("year");
		if(year==null)
		{
			year=Calendar.getInstance().get(Calendar.YEAR);
		}
		//实例化一个图形配置对象
		GraphConfig graphConfig = new GraphConfig();
		//主标题
		graphConfig.setCaption(year+"年度合同,预算,实际费用统计图表（单位：元）");
		//X坐标轴名称
		graphConfig.setXAxisName("费用名称");
		graphConfig.setYAxisName("费用金额");
		//数字值前缀
		//graphConfig.setNumberPrefix("$");
		List dataList = new ArrayList();
		
		
		
		//处理实际费用
		Dto shiJiMoney=(Dto)this.g4Reader.queryForObject("report.shijimoney", year);
		if(shiJiMoney.getAsInteger("total")==null)
		{
			Set set=new Set();
			set.setName(moneyname[0]);
			set.setValue("0");
			dataList.add(set);
		}
		else
		{
			Set set=new Set();
			set.setName(moneyname[0]);
			set.setValue(shiJiMoney.getAsString("total"));
			dataList.add(set);
		}
		
		
		
		//处理合同费用
		Dto heTongMoney=(Dto)this.g4Reader.queryForObject("report.hetongmoney", year);
		if(heTongMoney.getAsInteger("total")==null)
		{
			Set set=new Set();
			set.setName(moneyname[1]);
			set.setValue("0");
			dataList.add(set);
		}
		else
		{
			Set set=new Set();
			set.setName(moneyname[1]);
			set.setValue(heTongMoney.getAsString("total"));
			dataList.add(set);
		}
		
		
		//处理实际费用
		Dto yuSuanMoney=(Dto)this.g4Reader.queryForObject("report.yusuanmoney", year);
		if(yuSuanMoney.getAsInteger("total")==null)
		{
			Set set=new Set();
			set.setName(moneyname[2]);
			set.setValue("0");
			dataList.add(set);
		}
		else
		{
			Set set=new Set();
			set.setName(moneyname[2]);
			set.setValue(yuSuanMoney.getAsString("total"));
			dataList.add(set);
		}
		//将图表数据转为Flash能解析的XML资料格式
		String xmlString = FcfDataMapper.toFcfXmlData(dataList, graphConfig);
		return xmlString;
	}
	//查询合同，实际，预算费用数据并格式化成xml用于显示图表
	public ActionForward queryShiJiHeTongYuSuan(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("提示：正在修改图表参数。。。。。。");
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		//将图表数据转为Flash能解析的XML资料格式
		String xmlString = this.getXML2(dto);
		Dto outDto = new BaseDto();
		outDto.put("success", new Boolean(true));
		outDto.put("xmlstring", xmlString);
		write(JsonHelper.encodeObject2Json(outDto), response);
		return mapping.findForward(null);
	}
}
